﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace s7nck
{
    public partial class ShowDataWinform : Form
    {
        public ConcurrentDictionary<string, string> cache;
        
        private DataTable _tab;
        private BindingSource bindingSource1;
        public ShowDataWinform()
        {
            InitializeComponent();
        }

        public void showmachineIP(string name,string ip)
        {
            label2.Text = name;
            label4.Text = ip;
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Dispose();
        }

        private void ShowDataWinform_Load(object sender, EventArgs e)
        {
            
            //开启定时器
            timer1.Tick +=  new EventHandler(this.timer1_Tick);
             _tab = new DataTable();
             bindingSource1 = new BindingSource();
            
            timer1.Interval = 0x3e8;
            timer1.Start();
            _tab.Columns.Add("Key", typeof(string));
            _tab.Columns.Add("Value", typeof(string));
            _tab.PrimaryKey = new DataColumn[] { _tab.Columns[0] };
           bindingSource1.DataSource =_tab;

            dataGridView1.DataSource = bindingSource1;
            dataGridView1.Columns[0].Width = 200;
            dataGridView1.Columns[1].Width = 200;

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            try
            {
                foreach (KeyValuePair<string, string> pair in cache)
                {

                    object key = pair.Key;
                    DataRow objA = _tab.Rows.Find(key);
                    string str = (pair.Value == null) ? "" : pair.Value.ToString();
                    if (!ReferenceEquals(objA, null))
                    {
                        objA[1] = str;
                        continue;
                    }
                    object[] values = new object[] { pair.Key, str };
                    _tab.Rows.Add(values);


                }
            }
            catch (Exception ex)
            {

                LogHelper.WriteLog(ex.Message, ex);
            }

            
        }

    }
}
